﻿using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace TestApplication
{
    public partial class SettingsForm : Form
    {
        protected String ebmtDbConnectionString;
        protected String mappingInfoConnectionString;
        protected String agnisStagingConnectionString;
        SqlConnection myTestConnection;
        private FormContainer myFormContainer;

        public void SetFormContainer(FormContainer myFContainer)
        {
            myFormContainer = myFContainer;
        }
        public SettingsForm()
        {
            InitializeComponent();
            //LoadConfigurationSettings();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.Validate();
            if (TestConnectionString(ebmtDbConnectionString)
                && TestConnectionString(mappingInfoConnectionString)
                && TestConnectionString(agnisStagingConnectionString))
            { 
                SetUserDefinedConnectionStrings(ebmtDbConnectionString, mappingInfoConnectionString, agnisStagingConnectionString);
                LoadConfigurationSettings();
                myFormContainer.OpenTestForm();
            }
            this.Close();
        }

        private bool TestConnectionString(String connString)
        {
            try
            {
                myTestConnection = new SqlConnection(connString);
                myTestConnection.Open();
                myTestConnection.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        private void txtServerDBUSerAndPaswordName_Validating(object sender, CancelEventArgs e)
        {
            if (this.chkBoxSameUserId.Checked)
            {
                if (this.txtServerName.Text != "" &&
                this.txtEBMTDbName.Text != "" &&
                this.txtMappingInfoDbName.Text != "" &&
                this.txtUserName.Text != "" &&
                this.txtUserPassword.Text != "")
                {
                     ebmtDbConnectionString  = "packet size=4096;data source="+ this.txtServerName.Text+
                                               ";initial catalog=" + this.txtEBMTDbName.Text +
                                               ";persist security info=True" +
                                               ";user id=" + this.txtUserName.Text + 
                                                ";password=" + this.txtUserPassword.Text ;
                     txtConnectionString.Text = ebmtDbConnectionString;
                     mappingInfoConnectionString =  "packet size=4096;data source=" + this.txtServerName.Text +
                                                    ";initial catalog=" + this.txtMappingInfoDbName.Text +
                                                    ";persist security info=True" +
                                                    ";user id=" + this.txtUserName.Text +
                                                    ";password=" + this.txtUserPassword.Text;
                     txtConnectionString2.Text = mappingInfoConnectionString;
                     agnisStagingConnectionString  = "packet size=4096;data source=" + this.txtServerName.Text +
                                                     ";initial catalog=" + this.txtAgnisStagingName.Text +
                                                     ";persist security info=True" +
                                                     ";user id=" + this.txtUserName.Text +
                                                     ";password=" + this.txtUserPassword.Text;
                     txtConnectionString3.Text = agnisStagingConnectionString;
                }
            }
            else
            {
                if (this.txtServerName.Text != "" &&
                this.txtEBMTDbName.Text != "" &&
                this.txtMappingInfoDbName.Text != "" &&
                this.txtUserName.Text != "" &&
                this.txtUserName2.Text != "" &&
                this.txtUserPassword.Text != "" &&
                this.txtUserPassword2.Text != "")
                {
                    ebmtDbConnectionString = "packet size=4096;data source=" + this.txtServerName.Text +
                                              ";initial catalog=" + this.txtEBMTDbName.Text +
                                              ";persist security info=True" +
                                              ";user id=" + this.txtUserName.Text +
                                               ";password=" + this.txtUserPassword.Text;
                    txtConnectionString.Text = ebmtDbConnectionString;
                    mappingInfoConnectionString = "packet size=4096;data source=" + this.txtServerName.Text +
                                                   ";initial catalog=" + this.txtMappingInfoDbName.Text +
                                                   ";persist security info=True" +
                                                   ";user id=" + this.txtUserName2.Text +
                                                   ";password=" + this.txtUserPassword2.Text;
                    txtConnectionString2.Text = mappingInfoConnectionString;
                    agnisStagingConnectionString = "packet size=4096;data source=" + this.txtServerName.Text +
                                                    ";initial catalog=" + this.txtAgnisStagingName.Text +
                                                    ";persist security info=True" +
                                                    ";user id=" + this.txtUserName3.Text +
                                                    ";password=" + this.txtUserPassword3.Text;
                    txtConnectionString3.Text = agnisStagingConnectionString;
                }
            }
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {
            LoadConfigurationSettings();
        }

        private void btnResetConnections_Click(object sender, EventArgs e)
        {
            this.txtServerName.Text = "";
            this.txtEBMTDbName.Text = "";
            this.txtMappingInfoDbName.Text = "";
            this.txtAgnisStagingName.Text = "";
            this.txtUserName.Text = "";
            this.txtUserName2.Text = "";
            this.txtUserName3.Text = "";
            this.txtUserPassword.Text = "";
            this.txtUserPassword2.Text = "";
            this.txtUserPassword3.Text = "";
            this.chkBoxSameUserId.Checked = false;
            LoadConfigurationSettings();
        }

        public void LoadConfigurationSettings()
        {
            System.Configuration.Configuration config =
                        ConfigurationManager.OpenExeConfiguration(
                        ConfigurationUserLevel.None);

            System.Configuration.KeyValueConfigurationElement myEbmtDbKey =
                config.AppSettings.Settings["userDefinedEBMTDbConnectionString"];
            System.Configuration.KeyValueConfigurationElement myMappingInfoKey =
                config.AppSettings.Settings["userDefinedMappingInfoConnectionString"];
            System.Configuration.KeyValueConfigurationElement myAgnisStagingKey =
               config.AppSettings.Settings["userDefinedAgnisStagingConnectionString"];
            if (myEbmtDbKey.Value != "" && myMappingInfoKey.Value != "" & myAgnisStagingKey.Value!="")
            {
                ReadUserDefinedConnectionStrings(myEbmtDbKey, myMappingInfoKey, myAgnisStagingKey);
            }
            else
            {
                ReadDefaultConnectionStrings();
            }
        }

        private void ReadDefaultConnectionStrings()
        {
        ConnectionStringSettingsCollection connections =
            ConfigurationManager.ConnectionStrings;

            if (connections.Count != 0)
            {
                // Get the collection elements.
                 foreach (ConnectionStringSettings connection in
                  connections)
                {
                    string name = connection.Name;
                    if (connection.Name.IndexOf("EBMTDb")>-1)
                    {
                        ebmtDbConnectionString = connection.ConnectionString;
                        txtConnectionString.Text  = ebmtDbConnectionString;
                    }
                    if (connection.Name.IndexOf("Mapping") > -1)
                    {
                        mappingInfoConnectionString  = connection.ConnectionString;
                        txtConnectionString2.Text = mappingInfoConnectionString;
                    }
                    if (connection.Name.IndexOf("AGNIS_FORMS") > -1)
                    {
                        agnisStagingConnectionString = connection.ConnectionString;
                        txtConnectionString3.Text = agnisStagingConnectionString;
                    }
                }
            }
        }

        private void ReadUserDefinedConnectionStrings(System.Configuration.KeyValueConfigurationElement ebmtDbKey,
            System.Configuration.KeyValueConfigurationElement mappingInfoKey, System.Configuration.KeyValueConfigurationElement agnisStagingKey)
        {
            ebmtDbConnectionString = ebmtDbKey.Value;
            txtConnectionString.Text = ebmtDbKey.Value;

            mappingInfoConnectionString = mappingInfoKey.Value ;
            txtConnectionString2.Text = mappingInfoKey.Value;

            agnisStagingConnectionString=  agnisStagingKey.Value;
            txtConnectionString3.Text = agnisStagingKey.Value;
        }

        private void SetUserDefinedConnectionStrings(String ebmtDbConnectionString, 
                                                     String mappingInfoConnectionString,
                                                     String agnisStagingConnectionString)
        {
            System.Configuration.Configuration config =
                     ConfigurationManager.OpenExeConfiguration(
                     ConfigurationUserLevel.None);
                config.AppSettings.Settings["userDefinedEBMTDbConnectionString"].Value= ebmtDbConnectionString;
                config.AppSettings.Settings["userDefinedMappingInfoConnectionString"].Value = mappingInfoConnectionString;
                config.AppSettings.Settings["userDefinedAgnisStagingConnectionString"].Value = agnisStagingConnectionString;
            config.Save(ConfigurationSaveMode.Full);
        }

        private void SetEbmtDbConnectionStrings(String ebmtDbConnectionString)
        {
            System.Configuration.Configuration config =
                     ConfigurationManager.OpenExeConfiguration(
                     ConfigurationUserLevel.None);
            config.AppSettings.Settings["userDefinedEBMTDbConnectionString"].Value = ebmtDbConnectionString;            
            config.Save(ConfigurationSaveMode.Full);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void chkBoxSameUserId_CheckedChanged(object sender, EventArgs e)
        {
            if (chkBoxSameUserId.Checked)
            {
                this.txtUserName2.Text = "";
                this.txtUserPassword2.Text="";
                this.txtUserName2.Enabled = false;
                this.txtUserPassword2.Enabled = false;
                this.txtUserName3.Text = "";
                this.txtUserPassword3.Text = "";
                this.txtUserName3.Enabled = false;
                this.txtUserPassword3.Enabled = false;
            }
            else
            {
                this.txtUserName2.Enabled = true;
                this.txtUserPassword2.Enabled = true;
                this.txtUserName3.Enabled = true;
                this.txtUserPassword3.Enabled = true;
            }
        }

        private void btnTestConnection_Click(object sender, EventArgs e)
        {
            btnTestConnection.Enabled = false;
            if (TestConnectionString(ebmtDbConnectionString))
            { MessageBox.Show("Connection succeded."); }
            else
            { MessageBox.Show("Connection failed."); }
            btnTestConnection.Enabled = true;
        }

        private void btnTestConnection2_Click(object sender, EventArgs e)
        {
            btnTestConnection2.Enabled = false;
            if (TestConnectionString(mappingInfoConnectionString))
            { MessageBox.Show("Connection succeded."); }
            else
            { MessageBox.Show("Connection failed."); }
            btnTestConnection2.Enabled = true;
        }

        private void btnTestConnection3_Click(object sender, EventArgs e)
        {
            btnTestConnection3.Enabled = false;
            if (TestConnectionString(agnisStagingConnectionString))
            { MessageBox.Show("Connection succeded."); }
            else
            { MessageBox.Show("Connection failed."); }
            btnTestConnection3.Enabled = true;
        }

        private void txtServerDBUSerAndPaswordName_TextChanged(object sender, EventArgs e)
        {
           /* if (this.chkBoxSameUserId.Checked)
            {
                if (this.txtServerName.Text != "" ||
                this.txtEBMTDbName.Text != "" ||
                this.txtMappingInfoDbName.Text != "" ||
                this.txtUserName.Text != "" ||
                this.txtUserPassword.Text != "")
                {
                    ebmtDbConnectionString = "packet size=4096;data source=" + this.txtServerName.Text +
                                              ";initial catalog=" + this.txtEBMTDbName.Text +
                                              ";persist security info=True" +
                                              ";user id=" + this.txtUserName.Text +
                                               ";password=" + this.txtUserPassword.Text;
                    txtConnectionString.Text = ebmtDbConnectionString;
                    mappingInfoConnectionString = "packet size=4096;data source=" + this.txtServerName.Text +
                                                   ";initial catalog=" + this.txtMappingInfoDbName.Text +
                                                   ";persist security info=True" +
                                                   ";user id=" + this.txtUserName.Text +
                                                   ";password=" + this.txtUserPassword.Text;
                    txtConnectionString2.Text = mappingInfoConnectionString;
                    agnisStagingConnectionString = "packet size=4096;data source=" + this.txtServerName.Text +
                                                    ";initial catalog=" + this.txtAgnisStagingName.Text +
                                                    ";persist security info=True" +
                                                    ";user id=" + this.txtUserName.Text +
                                                    ";password=" + this.txtUserPassword.Text;
                    txtConnectionString3.Text = agnisStagingConnectionString;
                }
            }
            else
            {
                if (this.txtServerName.Text != "" ||
                this.txtEBMTDbName.Text != "" ||
                this.txtMappingInfoDbName.Text != "" ||
                this.txtUserName.Text != "" ||
                this.txtUserName2.Text != "" ||
                this.txtUserPassword.Text != "" ||
                this.txtUserPassword2.Text != "")
                {
                    ebmtDbConnectionString = "packet size=4096;data source=" + this.txtServerName.Text +
                                              ";initial catalog=" + this.txtEBMTDbName.Text +
                                              ";persist security info=True" +
                                              ";user id=" + this.txtUserName.Text +
                                               ";password=" + this.txtUserPassword.Text;
                    txtConnectionString.Text = ebmtDbConnectionString;
                    mappingInfoConnectionString = "packet size=4096;data source=" + this.txtServerName.Text +
                                                   ";initial catalog=" + this.txtMappingInfoDbName.Text +
                                                   ";persist security info=True" +
                                                   ";user id=" + this.txtUserName2.Text +
                                                   ";password=" + this.txtUserPassword2.Text;
                    txtConnectionString2.Text = mappingInfoConnectionString;
                    agnisStagingConnectionString = "packet size=4096;data source=" + this.txtServerName.Text +
                                                    ";initial catalog=" + this.txtAgnisStagingName.Text +
                                                    ";persist security info=True" +
                                                    ";user id=" + this.txtUserName3.Text +
                                                    ";password=" + this.txtUserPassword3.Text;
                    txtConnectionString3.Text = agnisStagingConnectionString;
                }
            }
            */
        }

        private void cbLeidenDb_CheckedChanged(object sender, EventArgs e)
        {
            //reset all parameter values for EBMT_Database cnx
            string server = "145.88.211.27";
            string ebmtDb = "TEST_MEDAB_";
            string UserName = "EBMT_AGNIS";
            string passwd = "m69$dKSlz";

            if (cbLeidenDb.Checked)
            {
                txtServerName.Text = server;
                txtEBMTDbName.Text = ebmtDb;
                txtUserName.Text = UserName;
                txtUserPassword.Text = passwd;

                ebmtDbConnectionString = "packet size=4096;data source=" + server +
                                                  ";initial catalog=" + ebmtDb +
                                                  ";persist security info=True" +
                                                  ";user id=" + UserName +
                                                   ";password=" + passwd;
                if (!TestConnectionString(ebmtDbConnectionString))
                {
                    MessageBox.Show("Connexion failed to EBMT Database!", "Something's wrong with Database");
                }
                else
                {
                    SetEbmtDbConnectionStrings(ebmtDbConnectionString);
                }
            }
            else
            {
                txtServerName.Text = "";
                txtEBMTDbName.Text = "";
                txtUserName.Text = "";
                txtUserPassword.Text = "";
            }
        }

        
    }
}
